﻿using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using MyMVCDemo.Models.EF;
using MyMVCDemo.Models.ViewModels.Member;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyMVCDemo.Services
{
    public class AddressServices
    {
        private readonly MiniQQContext _context;
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly AccountServices _accountServices;

        public AddressServices(MiniQQContext context, IHttpContextAccessor httpContextAccessor, AccountServices accountServices)
        {
            _context = context;
            _httpContextAccessor = httpContextAccessor;
            _accountServices = accountServices;
        }

        /// <summary>
        /// 返回指定id的用户通讯录记录  
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IEnumerable<T001好友表>>GetListByHostId(string id)
        {
            var model = await _context.T001好友表s.Where(m => m.Qq号 == id).ToListAsync();
            return model;
        }
        /// <summary>
        /// 搜索指定用户的通讯录记录
        /// </summary>
        /// <param name="id"></param>
        /// <param name="searchTxt">搜索条件</param>
        /// <returns></returns>
        public async Task<IEnumerable<T001好友表>> SearchListByHostId(string id, string searchTxt)
        {
            var model = await _context.T001好友表s.Where(m => m.Qq号 == id
            && (m.姓名.Contains(searchTxt)
            || m.电话.Contains(searchTxt))).ToArrayAsync();
            return model;
        }

        /// <summary>
        /// 创建新的记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task Create(AddressCreateModel model)
        {
            var newItem = new T001好友表()
            {
                Id = Guid.NewGuid(),
                Qq号 = _accountServices.MemberQq,
                电话 = model.电话,
                姓名 = model.姓名,
            };
            await _context.AddAsync(newItem);//暂存
            await _context.SaveChangesAsync();//存到硬盘
        }

        /// <summary>
        /// 返回指定key的详细记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T001好友表> Detail(Guid id)
        {
            var item = await _context.T001好友表s.FindAsync(id);//找不到的话会返回null
            //如果不是该用户的好友，返回null
            if (item != null && item.Qq号 != _accountServices.MemberQq)
                item = null;
            return item;
        }

        public async Task<AddressEditModel> GeteEditModel(Guid id)
        {
            var item = await _context.T001好友表s.FindAsync(id);//找不到的话会返回null
            AddressEditModel model = new AddressEditModel
            {
                Id = id,
                姓名 = item.姓名,
                电话 = item.电话,
            };
            return model;
        }

        /// <summary>
        /// 编辑记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task Edit(AddressEditModel model)
        {
            var item = await _context.T001好友表s.FindAsync(model.Id);//找不到的话会返回null

            item.姓名 = model.姓名;
            item.电话 = model.电话;

            await _context.SaveChangesAsync();//存到硬盘
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task Delete(Guid id)
        {
            var item = await _context.T001好友表s.FindAsync(id);//找不到的话会返回null
            //如果不是该用户的好友，返回null
            if (item != null && item.Qq号 != _accountServices.MemberQq)
                return;
            _context.T001好友表s.Remove(item);
            await _context.SaveChangesAsync();
        }
    }
}
